<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2>登录</h2>
      <el-form :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            type="password"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
  
  
  <script setup>
import { useRouter } from "vue-router";
import axios from "axios";
import { ElMessage } from "element-plus";
import { reactive } from "vue";

const router = useRouter();
const form = reactive({
  username: "",
  password: "",
});

const handleLogin = async () => {
  const res = await axios.post("/api/login", form);
  if (res.data.code === 200) {
    ElMessage.success(res.data.message);
    localStorage.setItem("token", res.data.token);
    router.push("/students");
  } else {
    ElMessage.error(res.data.message);
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #f5f5f5;
}

.login-card {
  width: 300px;
  padding: 20px;
  text-align: center;
}
</style>
  